package com.twu.biblioteca;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Map;
import java.util.TreeMap;

public class Menu implements MenuItem {
    private Map<Character, MenuItem> options;
    private Map<Integer, String> library;
    private InteractiveConsole console;

    public Menu(InteractiveConsole console, Map<Integer, String> library) {
        this.console = console;
        this.library = library;

        initializeOptions();
    }

    @Override
    public void start() throws IOException {
        console.println("Welcome to the robo-bibliotec!");

        printMenu();
        select(console.read());
    }

    private void printMenu() {
        for (Map.Entry<Character, MenuItem> option : options.entrySet()) {
            console.println(String.format("(%s) %s", option.getKey(), option.getValue()));
        }
    }

    private void initializeOptions() {
        options = new TreeMap<Character, MenuItem>();

        options.put('l', new ListMenuItem(console, library));
        options.put('r', new ReserveMenuItem(console, library));
        options.put('m', new MembershipMenuItem(console));
    }

    private void select(Character choice) throws IOException {
        if (options.containsKey(choice)) {
            options.get(choice).start();
        } else {
            console.println("Select a valid option!!");
        }
    }
}
